package com.niren.relay.webController.relay;

import com.niren.relay.common.constant.constant;
import com.niren.relay.common.devinfo.AddDevInfo;
import com.niren.relay.common.devinfo.SetDevState;
import com.niren.relay.common.regularExpress.RegularExpress;
import com.niren.relay.redis.RedisString;
import com.niren.relay.service.redisservice.SysDeviceManageService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dev")
public class relayController {

    @RequestMapping("/adddev")
    public Map<String,Object> addDevice(
            @RequestBody AddDevInfo devInfo
    ){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        SysDeviceManageService sdmService = new SysDeviceManageService();
        //System.out.println(devInfo);
        Boolean ret = sdmService.CreateDeviceByDevInfo(devInfo);
        modelMap.put("STATUS",ret);//状态码为200，并为true时，才算正确
        return modelMap;
    }

    @RequestMapping("/deldev")
    public Map<String,Object> delDevice(){
        Map<String,Object> modelMap = new HashMap<String,Object>();

        return modelMap;
    }

    @RequestMapping("/getdev")
    public Map<String,Object> getDev(){//通过此接口，要以获取到所有数据，包括实时值
        SysDeviceManageService sdmService = new SysDeviceManageService();
        return sdmService.getAllDevInfo();
    }

    @RequestMapping("/setdev")
    public Map<String,Object> setDev(
            @RequestBody SetDevState setDevState
    ){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        if(setDevState.getControlRange() == null
        || setDevState.getMacOrKey() == null
        || setDevState.getState() == null
        || setDevState.getBlinkTime() == null){
            modelMap.put("STATUS","null value");
            return modelMap;
        }

        //判断控制范围有效值
        if(setDevState.getControlRange() > 1 || setDevState.getControlRange() < 0){
            modelMap.put("STATUS","controlRange value error");
            return modelMap;
        }
        //判断mac的有效性
        /*if(RegularExpress.stringIsMac(setDevState.getMacOrKey())){
            modelMap.put("STATUS","macOrKey value error");
            return modelMap;
        }*/
        //判断state的有效性
        if(setDevState.getState()!=0 && setDevState.getState() != 1){
            modelMap.put("STATUS","state value error");
            return modelMap;
        }
        //判断blinkTime的有效性
        if(setDevState.getBlinkTime() <0 || setDevState.getBlinkTime() > 100000){
            modelMap.put("STATUS","blinkTime value error");
            return modelMap;
        }
        //controlRange为1时，macOrKey 为 key
        if(setDevState.getControlRange() == 1 && setDevState.getMacOrKey().contains("|") == false){
            modelMap.put("STATUS","macOrKey format error");
            return modelMap;
        }

        if(macIsExist(setDevState) == false){
            modelMap.put("STATUS","mac inexistence");//mac不存在
            return modelMap;
        }

        constant.setStateQueue.InQueue(setDevState);
        modelMap.put("STATUS","OK");
        return modelMap;
    }

    private boolean macIsExist(SetDevState setDevState){
        RedisString rString = new RedisString(constant.hostOrIp);
        String macsStr = rString.getValue(constant.macsKey);
        String mac = setDevState.getMacOrKey();
        if(mac.contains("|") == true){
            mac = mac.split("|")[0];
        }
        return macsStr.contains(mac);
    }


}
